<?php
class Catalog_Helper
{
	private $id;
	private $info;
	public function Catalog_Helper()
	{
		$this->id = 0;
		$this->info = null;
	}
	public function init($db_id)
	{
		$this->id = intval($db_id);
		$dtCatalog = new DataTable(constant('DB_TABLE_CATALOG'));
		$info = $dtCatalog->get($this->id);
		if($info == null)
			throw new Exception("catalog id:".$this->id." not exist");
	$size_list = array();
	$size_frame_list = new stdClass();
	//load size info
	$dtPhotoSize = new DataTable(constant('DB_TABLE_PHOTO_SIZE'));
	$dtPhotoSize->primaryKeyAsKey = true;
	$photoSizeMap = $dtPhotoSize->get_list();
	
	$dtPhotoFrame = new DataTable(constant('DB_TABLE_PHOTO_FRAME'));
	$dtPhotoFrame->primaryKeyAsKey = true;
	$photoFrameMap = $dtPhotoFrame->get_list();
	
	$dtCatPhotoSize = new DataTable(constant('DB_TABLE_CATALOG_PHOTO_SIZE'));
	$cat_size_list = $dtCatPhotoSize->get_list(sprintf("`catalog_id`=%d",$info['id']));
	foreach($cat_size_list as $index=>$tmpData)
	{
		if(!array_key_exists($tmpData['photo_size_id'],$photoSizeMap))
			continue;
		$tmpObj = new stdClass();
		$tmpObj->size_id = $tmpData['photo_size_id'];
		$tmpObj->size_name = $photoSizeMap[$tmpData['photo_size_id']]['name'];
		$tmpObj->number = $tmpData['number'];
		$size_list[] = $tmpObj;
		
		//load size frame
		$dtCatPhotoSizeFrame = new DataTable(constant('DB_TABLE_CATALOG_PHOTO_SIZE_FRAME'));
		$tmp_size_frame_list = $dtCatPhotoSizeFrame->get_list(sprintf("`cps_id`=%d",$tmpData['id']),'id');
		$size_frame_list->$tmpData['photo_size_id'] = array();
		foreach($tmp_size_frame_list as $tmpFrameData)
		{
			if(!array_key_exists($tmpFrameData['frame_id'],$photoFrameMap))
				continue;
			$tmpSizeFrameObj = new stdClass();
			$tmpSizeFrameObj->frame_id = $tmpFrameData['frame_id'];
			$tmpSizeFrameObj->frame_name = $photoFrameMap[$tmpFrameData['frame_id']]['name'];
			$ref_size_frame_obj = &$size_frame_list->$tmpData['photo_size_id'];
			$ref_size_frame_obj[] = $tmpSizeFrameObj;
		}
	}
	$info['photo_size'] = $size_list;

		
	$info['photo_size_frame'] = $size_frame_list;
	//load catalog gift
	$dtGift = new DataTable(constant('DB_TABLE_GIFT'));
	$dtGift->primaryKeyAsKey = true;
	$dtCatGift = new DataTable(constant('DB_TABLE_CATALOG_GIFT'));
	$giftMap = $dtGift->get_list();
	$cat_gift_list = $dtCatGift->get_list(sprintf("`catalog_id`=%d",$info['id']));
	$gift_list = array();
	foreach($cat_gift_list as $tmpData)
	{
		if(!array_key_exists($tmpData['gift_id'],$giftMap))
			continue;
		$tmpObj = new stdClass();
		$tmpObj->gift_id = $tmpData['gift_id'];
		$tmpObj->gift_name = $giftMap[$tmpData['gift_id']]['name'];
		$tmpObj->number = $tmpData['number'];
		$gift_list[] = $tmpObj;
	}
	$info['gift'] = $gift_list;
	$this->info = $info;
	}
	
	public function setInfo($inData)
	{
	$dtCatalog = new DataTable(constant('DB_TABLE_CATALOG'));
$dtCatPhotoSize = new DataTable(constant('DB_TABLE_CATALOG_PHOTO_SIZE'));
$dtCatPhotoSizeFrame = new DataTable(constant('DB_TABLE_CATALOG_PHOTO_SIZE_FRAME'));
$dtCatGift = new DataTable(constant('DB_TABLE_CATALOG_GIFT'));
		$json = new Services_JSON();
		$r_size_table = $json->decode(Util::work_str($inData['_size_table'],2));
	$r_size_frame_table = $json->decode(Util::work_str($inData['_size_frame_table'],2));
	$r_gift_table = $json->decode(Util::work_str($inData['_gift_table'],2));
	
	$info = $dtCatalog->save($inData);
	//size list
	$cat_size_list = $dtCatPhotoSize->get_list(sprintf("`catalog_id`=%d",$info['id']));
	foreach($cat_size_list as $tmpData)
	{
		$dtCatPhotoSizeFrame->removeByCondition(sprintf("`cps_id`=%d",$tmpData['id']));
		$dtCatPhotoSize->remove($tmpData['id']);
	}
	foreach($r_size_table as $tmpData)
	{
		$sizeData = array('catalog_id'=>$info['id'],'photo_size_id'=>$tmpData->size_id,'number'=>$tmpData->number);
		$sizeData = $dtCatPhotoSize->save($sizeData);
		//insert size frame info
		$size_frame_list = $r_size_frame_table[$tmpData->size_id];
		foreach($size_frame_list as $tmpData2)
		{
			$sizeFrameData = array('cps_id'=>$sizeData['id'],'frame_id'=>$tmpData2->frame_id);
			$dtCatPhotoSizeFrame->save($sizeFrameData);
		}
	}
	//gift list
	$dtCatGift->removeByCondition(sprintf("`catalog_id`=%d",$info['id']));
	foreach($r_gift_table as $tmpData)
	{
		$giftData = array('catalog_id'=>$info['id'],'gift_id'=>$tmpData->gift_id,'number'=>$tmpData->number);
		$dtCatGift->save($giftData);
	}
	}
	public function getInfo()
	{
		return $this->info;
	}
}
?>